// Copyright 2022 Hollson. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package leetcode1

import (
	"fmt"
	"sort"
)

// https://www.cnblogs.com/AndyJee/p/4537251.html
// http://t.zoukankan.com/liuwt365-p-7222549.html
// https://blog.csdn.net/Mister_SNAIL/article/details/77860240
// https://programmercarl.com/0435.%E6%97%A0%E9%87%8D%E5%8F%A0%E5%8C%BA%E9%97%B4.html

func eraseOverlapIntervals(intervals [][]int) int {
	var flag int
	// 先排序
	sort.Slice(intervals, func(i, j int) bool {
		return intervals[i][0] < intervals[j][0]
	})
	fmt.Println(intervals)
	for i := 1; i < len(intervals); i++ {
		if intervals[i-1][1] > intervals[i][0] {
			flag++
			intervals[i][1] = min(intervals[i-1][1], intervals[i][1])
			// 由于是先排序的，所以，第一位是递增顺序，故只需要将临近两个元素的第二个值最小值更新到该元素的第二个值即可作之后的判断
		}
	}
	return flag
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
